var app = getApp();

let col1H = 0;
let col2H = 0;

Page({
  data:{
    indicatorDots :false,
    autoplay : true,
    interval : 3000,
    duration : 1000,

        scrollH: 0,
        imgWidth: 0,
        loadingCount: 0,
        images: [],
        col1: [],
        col2: []
  },
  onLoad:function(options){
      app.cid = options.cid;
        wx.getSystemInfo({
            success: (res) => {
                let ww = res.windowWidth;
                let wh = res.windowHeight;
                let imgWidth = ww * 0.48;
                let scrollH = wh;
                // console.log(wh);
                this.setData({
                    scrollH: scrollH,
                    imgWidth: imgWidth
                });

                //加载首组图片
                this.loadImages();
            }
        })
  },
  onImageLoad: function (e) {
        // console.log(e);
        let imageId = e.currentTarget.id;
        // console.log(imageId)
        let oImgW = e.detail.width;         //图片原始宽度
        let oImgH = e.detail.height;        //图片原始高度
        let imgWidth = this.data.imgWidth;  //图片设置的宽度
        let scale = imgWidth / oImgW;        //比例计算
        let imgHeight = oImgH * scale;      //自适应高度

        let images = this.data.images;
        // console.log(this.data.images)
        let imageObj = null;

        for (let i = 0; i < images.length; i++) {
            let img = images[i];
            if (img.id === imageId) {
                imageObj = img;
                break;
            }
        }

        imageObj.height = imgHeight;

        let loadingCount = this.data.loadingCount - 1;
        let col1 = this.data.col1;
        // console.log(col1);
        let col2 = this.data.col2;

        //判断当前图片添加到左列还是右列
        if (col1H <= col2H) {
            col1H += imgHeight;
            col1.push(imageObj);
        } else {
            col2H += imgHeight;
            col2.push(imageObj);
        }

        let data = {
            loadingCount: loadingCount,
            col1: col1,
            col2: col2
        };

        //当前这组图片已加载完毕，则清空图片临时加载区域的内容
        if (!loadingCount) {
            data.images = [];
        }

        this.setData(data);
    },
    loadImages: function () {
      var that = this;
      wx.request({
          url: 'https://image.wertp.cn/api/photo/nav/cid/'+app.cid, 
          header: {
              'content-type': 'application/json'
          },
          success: function(res) {
            var data =res.data;
            // console.log(data.data)
            var images = [];
            for(var a = 0;a<data.data.content.data.length;a++){
              // console.log(data.data.content[a].thumb);
               var dd = [
                   { pic: "https://image.wertp.cn"+data.data.content.data[a].thumb, height: 0,tid:data.data.content.data[a].id,author:data.data.content.data[a].author,title:data.data.content.data[a].title },
               ];
            //    console.log(dd);
              images.push(dd[0]);
            }
            //  console.log(images);
            let baseId = "img-" + (+new Date());
            //  console.log(images.length);
            for (var i = 0; i < images.length; i++) {
                    images[i].id = baseId + "-" + i;
                }
                // console.log(images);

            that.setData({
                banner:data.data.banner,
                nav:data.data.nav,
                loadingCount: images.length,
                images: images
                });
          }
        })
        
        
    },

  onReady:function(){
  },
  onShow:function(){
  },
  onHide:function(){
  },
  onUnload:function(){
  },
  onPullDownRefresh: function() {
  },
  onReachBottom: function() {
  },
  onShareAppMessage: function() {
    return {
      title: 'title', // 分享标题
      desc: 'desc', // 分享描述
      path: 'path' // 分享路径
    }
  }
})